﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QMS.Core.Common;
using QMS.Core.Model;
using DoctorClient.Common;

namespace DoctorClient
{
    public partial class SettingForm : Form
    {
        static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public SettingForm()
        {
            InitializeComponent();
        }

        private List<DeptInfo> deptInfoList = null;
        private List<RoomInfo> roomInfoList = null;
        private List<DoctorInfo> doctorInfoList = null;
        private void SettingForm_Load(object sender, EventArgs e)
        {
            try
            {

                //查询科室、诊室、医生
                using (SQLContext db = new SQLContext())
                {
                    deptInfoList = db.DeptInfos.Where(o => o.Status == 1).ToList();
                    roomInfoList = db.RoomInfos.Where(o => o.Status == 1).ToList();
                    doctorInfoList = db.DoctorInfos.Where(o => o.Status == 1).ToList();
                }

                if (deptInfoList != null && deptInfoList.Count > 0)
                {
                    this.cboDept.ValueMember = "DeptCode";
                    this.cboDept.DisplayMember = "DeptName";
                    this.cboDept.DataSource = deptInfoList;
                }

                //读取配置文件
                ClientConfig.LoadConfig();

                this.cboDept.SelectedValue = ClientConfig.Dept;
                this.cboRoom.SelectedValue = ClientConfig.Room;
                this.cboDoctor.SelectedValue = ClientConfig.Doctor;
                this.cbNotify.Checked = ClientConfig.Notify;

            }
            catch (Exception ex)
            {
                log.Error("初始化设置异常！", ex);
                MessageBox.Show("初始化设置异常！原因：" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {

                string dept = Convert.ToString(this.cboDept.SelectedValue);
                int room = Convert.ToInt32(this.cboRoom.SelectedValue != null ? this.cboRoom.SelectedValue : 0);
                string doctor = Convert.ToString(this.cboDoctor.SelectedValue);
                bool notify = this.cbNotify.Checked;

                if (String.IsNullOrEmpty(dept))
                {
                    MessageBox.Show("请选择一个科室！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                if (room == 0)
                {
                    MessageBox.Show("请选择一个诊室！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                if (String.IsNullOrEmpty(doctor))
                {
                    MessageBox.Show("请选择一个医生！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                ClientConfig.Dept = dept;
                ClientConfig.Room = room;
                ClientConfig.Doctor = doctor;
                ClientConfig.Notify = notify;

                ClientConfig.SaveConfig();

                MessageBox.Show("保存配置成功！如果刷新失败请重启客户端！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            catch (Exception ex)
            {
                log.Error("保存设置异常！", ex);
                MessageBox.Show("保存设置异常！原因：" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cboDept_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string text = this.cboDept.Text;
                string selectedText = this.cboDept.SelectedText;
                string selectedValue = Convert.ToString(this.cboDept.SelectedValue);
                object selectedItem = this.cboDept.SelectedItem;

                this.cboRoom.DataSource = null;
                if (roomInfoList != null && roomInfoList.Count > 0 && !String.IsNullOrEmpty(selectedValue))
                {
                    List<RoomInfo> tempRoomInfoList = roomInfoList.Where(o => o.Dept == selectedValue).ToList();
                    if (tempRoomInfoList != null && tempRoomInfoList.Count > 0)
                    {
                        this.cboRoom.ValueMember = "RoomCode";
                        this.cboRoom.DisplayMember = "RoomName";
                        this.cboRoom.DataSource = tempRoomInfoList;
                    }
                }

            }
            catch (Exception ex)
            {
                log.Error("根据科室过滤诊室异常！", ex);
                MessageBox.Show("根据科室过滤诊室异常！原因：" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void cboRoom_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string dept = Convert.ToString(this.cboDept.SelectedValue);
                int room = Convert.ToInt32(this.cboRoom.SelectedValue != null ? this.cboRoom.SelectedValue : 0);

                this.cboDoctor.DataSource = null;
                if (doctorInfoList != null && doctorInfoList.Count > 0 && !String.IsNullOrEmpty(dept) && room != 0)
                {
                    List<DoctorInfo> tempDoctorInfoList = doctorInfoList.Where(o => o.Dept == dept && o.Room == room).ToList();
                    if (tempDoctorInfoList != null && tempDoctorInfoList.Count > 0)
                    {
                        this.cboDoctor.ValueMember = "DoctorCode";
                        this.cboDoctor.DisplayMember = "DoctorName";
                        this.cboDoctor.DataSource = tempDoctorInfoList;
                    }
                }
            }
            catch (Exception ex)
            {
                log.Error("根据诊室过滤医生异常！", ex);
                MessageBox.Show("根据诊室过滤医生异常！原因：" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

    }
}
